<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title" id="addModalLabel"><?php echo $uid ? "编辑" : "新增"; ?>管理员</h4>
</div>
<div class="modal-body">
    <form id="userform">
    <div class="form-group input-group  input-control">
        <span class="input-group-addon">用户名&nbsp;</span>
        <input type="text" class="form-control" id="uname" name="uname" placeholder="" onBlur="checkName(this.value);" value="<?php echo $uname; ?>">
    </div>
    <span class="span-name" id="anamecheck"></span>
    <div class="form-group input-group  input-control">
        <span class="input-group-addon">&nbsp;&nbsp;姓名&nbsp;&nbsp;</span>
        <input type="text" maxlength="11" class="form-control" id="nick" name="nick" placeholder="" value="<?php echo $nick; ?>">
    </div>
    <div class="form-group input-group  input-control">
        <span class="input-group-addon">&nbsp;&nbsp;手机&nbsp;&nbsp;</span>
        <input type="text" maxlength="11" class="form-control" id="phone" name="phone" placeholder="" value="<?php echo $phone; ?>">
    </div>
    <span class="span-phone" id="aphonecheck"></span>
    <div class="form-group input-group  input-control">
        <span class="input-group-addon">&nbsp;&nbsp;密码&nbsp;&nbsp;</span>
        <input type="password" class="form-control" id="password" name="password" placeholder="不修改密码请留空">
    </div>
    <div class="form-group input-group  input-control">
        <span class="input-group-addon">&nbsp;&nbsp;描述&nbsp;&nbsp;</span>
        <input type="text" class="form-control" id="description" name="description" placeholder="描述可为空">
    </div>
    <div class="form-group input-group  input-control">
        <label>用户角色</label>
        <label class="checkbox-inline">
        <input name="isadmin" type="radio" <?php echo $isadmin == '1'?'checked="checked"':''; ?> value="1" title="拥有系统全部权限"> 管理员
        </label>
        <label class="checkbox-inline">
        <input name="isadmin" type="radio" <?php echo ($isadmin == '0' || $isadmin === null)?'checked="checked"':''; ?> value="0" title="根据配置获取系统权限"> 普通用户
        </label>
    </div>
        <div class="form-group input-group  input-control">
            <label>验证方式</label>
            <label class="checkbox-inline">
                <input name="verify" type="radio" <?php echo ($verify === 1 || $verify === null)?'checked="checked"':''; ?> value="1" title="使用明道密码＋随机码验证"> 明道密码＋随机码
            </label>
            <label class="checkbox-inline">
                <input name="verify" type="radio"  <?php echo ($verify === 0)?'checked="checked"':''; ?> value="0" title="使用填入的密码做验证"> 系统密码
            </label>
        </div>
    <?php if(!empty($uid)) { ?>
    <div class="form-group input-group  input-control">
        <label>用户状态</label>
        <label class="checkbox-inline">
            <input id="status-normal" <?php echo $status == '0'?'checked="checked"':''; ?> name="status" type="radio" value="0"> 使用中
        </label>
        <label class="checkbox-inline">
            <input id="status-ban" <?php echo $status == '1'?'checked="checked"':''; ?> name="status" type="radio" value="1"> 已禁用
        </label>
    </div>
    <?php } ?>
    <input type="hidden" name="uid" value="<?php echo $uid; ?>" />
    </form>
</div>
<div class="modal-footer">
    <span class="tips" id="atip"></span>
    <button type="button" class="btn btn-default" data-dismiss="modal">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
    <button type="button" id="save" class="btn btn-primary">&nbsp;&nbsp;保存&nbsp;&nbsp;</button>
</div>
<script>
$(document).ready(function() {
    $('#save').click(function() {
        var uname=$('#uname'), nick=$('#nick'), phone=$('#phone'), password=$('#password'), description=$('#description');
        if (!uname.val().trim()) {
            alert('请填写用户名');
            uname.focus();
            return;
        }
        if (!nick.val().trim()) {
            alert('请填写姓名');
            nick.focus();
            return;
        }
        if (!phone.val().trim()) {
            alert('请填写手机号');
            phone.focus();
            return;
        }
        if (!/^1\d{10}$/.test(phone.val().trim())) {
            alert('请填写正确的手机号');
            phone.focus();
            return;
        }
        if(password.val() != '' && !/^\w{6,18}$/.test(password.val().trim())) {
            alert('密码仅允许是6~18位字母、数字');
            return;
        }
        var saveusr = url("/user/saveuserinfo/<?php echo isset($uid) ? '?uid='.$uid : ''; ?>");
        $.ajax({
            cache: true,
            type: "POST",
            url:saveusr,
            data:$('#userform').serialize(),
            dataType: 'json',
            error: function(data) {
                alert('系统异常，请稍后再试！');
            },
            success: function(data) {
                alert(data.msg);
                if(data.retcode == 2000000) {
                    refresh();
                }
            }
        });
    });

    //是否可以输入密码
    $('input[name="verify"]').click(function(){
        var verify = parseInt($(this).val());
        if(verify){
            $('input[name="password"]').attr('disabled',true);
        }else{
            $('input[name="password"]').attr('disabled',false);
        }
    });
    $('input[name="verify"]:checked').trigger('click');

});

// 检查用户名是否存在
function checkName(value){
    if('<?php echo $uname; ?>' == value) return;
	$.get(url("/user/checknameexist"), {
        name:value
    },
    function(data){
        if(data.retcode == 2000000){
            $('#anamecheck').html('');
        }else {
            $('#anamecheck').html('用户已存在');
        }
    },"json");
}
</script>
